package modelLayer;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Singleton container for Forest objects
 * 
 * @author Nick Pedersen
 * @version 0.1
 */
public class ForestContainer implements Serializable{
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Forest> forests;
    private static ForestContainer instance;

    /**
     * Constructor for objects of class ForestContainer
     */
    private ForestContainer(){
        forests = new ArrayList<Forest>();
    }

    /**
     * getInstance - Returns the instance of this container
     * @Return ForestContainer - Returns the instance of this container
     */
    public static ForestContainer getInstance(){
       if(instance == null){
           instance = new ForestContainer();
       }
       
       return instance;
    }
    
    /**
     * addForest - Adds a Forest object to the container
     * @Param Forest - The Forest object to be added
     */
    public void addForest(Forest f){
        forests.add(f);
    }
    
    /**
     * findForest - Finds and return an Forest object
     * @Param String name - The name of the Forest object
     * @Return Forest - The Forest object found
     */
    public Forest findForest(String name){
        Forest f = null;
        for(Forest forest : forests){
            if(forest.getName().toLowerCase().equals(name.toLowerCase())){
                f = forest;
            }
        }
        return f;
    }
    
    /**
     * containsTree - Finds the Forest object that contains the Tree object
     * @Param Tree - The tree owned by the Forest
     * @Return Forest - The Forest object that contains the Tree
     */
    public Forest containsTree(Tree tree){
        Forest reForest = null;
        for(Forest f : forests){
            if(f.containsTree(tree)){
                reForest = f;
            }
        }
        return reForest;
    }
    
    /**
     * getContainer - Returns the ArrayList of Forest's
     * @Return ArrayList<Forest> - The ArrayList with Forest Objects
     */
    public ArrayList<Forest> getContainer(){
        return forests;
    }

	public void remove(Forest f) {
		forests.remove(f);
	}
}
